package com.zhangji.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zhangji.springcloud.pojo.Dept;
import com.zhangji.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 提供Restful服务
 * @Author: Zhang Ji
 * @Date: 2021/6/22 20:45
 * Created by IntelliJ IDEA.
 */
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @HystrixCommand(fallbackMethod = "getDeptBackup")
    @GetMapping("/dept/get/{id}")
    public Dept getDept(@PathVariable("id") Long id){
        Dept dept = deptService.queryDeptById(id);
        if (dept == null) {
            throw new RuntimeException("id is null or userinfo is null!");
        }
        return dept;
    }

    public Dept getDeptBackup(@PathVariable("id") Long id){
        return new Dept().setDept_no(id).setDept_name("id is null or userinfo is null!").setDb_source("no database");
    }


}
